package org.example.cloudapply.controller;

import jakarta.validation.Valid;
import org.example.cloudapply.dto.WxLoginDTO;
import org.example.cloudapply.entity.Result;
import org.example.cloudapply.service.WechatAuthService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    private final WechatAuthService wechatAuthService;

    public AuthController(WechatAuthService wechatAuthService) {
        this.wechatAuthService = wechatAuthService;
    }


    @PostMapping("/login")
    public Result wxLogin(@Valid @RequestBody WxLoginDTO dto) {
        try {
            String openid = wechatAuthService.wechatLogin(dto);
            return Result.success(Map.of("openid", openid));
        } catch (Exception e) {
            return Result.error( e.getMessage());
        }
    }
}